<?php
/*
* 公共部分
* by JJ
× time 14.02.06
*/
class pubb{
	
	private $data;
	private $right;
	private $channel;
	
	private static $_instance;
	public static function get(){
		if(!(self::$_instance instanceof self)){
			self::$_instance = new self;
		}
		return self::$_instance;
	}
	
	//系统缓存	
	private function sys(){
		import('@.Class.mcache');
		$MCache = new MCache();
		$MCache->GetCache("home/nav");
		$MCache->GetCache("home/ad");	
		$MCache->GetCache("home/advertise");	
		$MCache->GetCache("home/newsname");
		$MCache->GetCache("home/catename2");
		global $NavCache,$AdCache,$AdvertiseCache,$NewsNameCache,$CateName2Cache;
		
		//News ID 对应 URL
		$this->data['nn'] = $NewsNameCache;
		$this->data['cn'] = $CateName2Cache;
	
		//底部
		$res = M("Advertise")->where("adcode='footer'")->field("adcontent")->find();
		$this->data['footer'] = $res['adcontent'];
		
		//幻灯片
		$this->data['ad'] = MCache::get('Ad/3');
		
		//首页幻灯
		$this->data['ad_index'] = MCache::get('Ad/4');
		
		//设置
		$SystemCache = MCache::get('channel/'.$this->channel);
		$this->channel_id = $SystemCache['id'];
		$SystemCache = unserialize($SystemCache['sys']);
		$title = $SystemCache['site_name'];
		$SystemCache['title'] = $title;
		$this->data['sys'] = $SystemCache;

		//广告图
		$adv = MCache::get('Advertise/'.$this->channel_id);
		$adv = array_merge($adv,MCache::get('Advertise/2'));
		$this->data['adv'] = $adv;
		
		//导航(全局公共)
		$PublicNav = MCache::get('Nav/index');
		//unset($PublicNav[0]);//干掉首页
		$this->data['PublicNav'] = $PublicNav;
		
		//私有导航
		$this->nav();
		
		//底部导航(全局公共)
		foreach($PublicNav as $v){
			if(strpos($v['nav_url'],$this->channel)){
				continue;
			}
			$PublicFooterNav[] = $v;	
		}
		$this->data['PublicFooterNav'] = $PublicFooterNav;
		
	}
	
	//导航
	private function nav(){
		$NavCache = MCache::get('Nav/'.$this->channel);
		$url = $_SERVER['REDIRECT_URL'];//当前URL
		//$array[] = $index;
		$array = array();
		$i=0;
		foreach($NavCache as &$v){
			if($url==$v['nav_url']){
				$v['current'] = 1;	
				$i=1;
			}
			$array[] = $v;
		}
		if($i==0){
			$array[0]['current'] = 1;
		}
		$this->data['nav'] = $array;
	}
	
	public function settitle($title){
		return $title.$this->data['sys']['site_meta_title'];	
	}
	
	public function getChannlId(){
		return $this->channel_id;	
	}
	
	public function setother($keywords,$description){
		if(!empty($keywords)){
			$this->data['sys']['site_meta_keywords'] = $keywords;
		}
		if(!empty($description)){
			$this->data['sys']['site_meta_description'] = $description;
		}
		return $this->data;
	}
	
	/*
	*	自定义设置其他属性
	*	p 地图 c 分类
	*/
	public function DiySetData($txt,$p,$c){
		$txt=str_replace("{p}",$p,$txt);
		$txt=str_replace("{c}",$c,$txt); 	
		return $txt;
	}
	
	/*
	*	提取100个字作描述
	*/
	public function getNumDesc($txt,$length=100){
		Load('extend');
		$txt = str_replace('&nbsp;','',strip_tags($txt));
		$txt = preg_replace("/[\s]{2,}/","",$txt); 
		$txt = str_replace("\n","",$txt);
		$txt = msubstr($txt,0,$length,'utf-8',false);
		return $txt;
	}
	
	public function setpassage($cid,$cname,$name){
		
		if($cid==23 || $cid==28){
			$url = U('nlists2/'.$cid);
		}elseif($cid==38){
			$url = U('success/index'); 
		}else{
			$url = U('nlists/'.$cid);	
		}	
		$array[0] = '<a href="/">加拿大留学首页</a>';
		$array[1] = '<a href="'.$url.'">'.$cname.'</a>';
		$array[2] = '<a href="'.__APP__.'">'.$name.'</a>';
		$p = '<span class="ml5 mr5">></span>';
		$res = implode($p,$array);
		return $res;
	}
	
	public function setChannel($channel){
		$this->channel = $channel;	
	}
	
	//公共部分
	public function run(){
		$this->sys();
		$this->partner();
		$this->links();
		return $this->data;	
	}
	
	//合作伙伴
	private function partner(){
		$this->data['Partner'] = M("Partner")->select();
	}
	
	//友情链接
	private function links(){
		$Links = MCache::get('Links/'.$this->channel_id);
		$this->data['Links'] = $Links;
	}
	
	//首页
	public function index(){

	}
	
	
	/*
	*	右侧模块
	*/
	//全球旭飞
	private function getSubsidiaries(){
		$globalxf = MCache::get('Setting/globalxf');
		$this->right['globalxf'] = $globalxf['c1'];
	}
	
	//资讯动态
	private function getRightNews(){
		$Information = M("Com_news")->where('cnid=2')->order('sorts desc,id desc')->field('id,nname,ndescription,add_time')->limit(5)->select();
		$this->right['information'] = $Information;	
	}
	
	//投资移民 && 技术移民 && 案例
	private function getRightProject(){
		$touzi = M("Yimin")->where('cid=1')->order('sorts desc,id desc')->limit(4)->field('id,pname')->select();
		$this->right['touzi'] = $touzi;	
		
		$jishu = M("Yimin")->where('cid=2')->order('sorts desc,id desc')->limit(4)->field('id,pname')->select();
		$this->right['jishu'] = $jishu;	
		
		$anLi = M("Com_anli")->where('cnid=2')->order('sorts desc,id desc')->field('id,nname,ndescription,add_time')->limit(2)->select();
		$this->right['anLi'] = $anLi;	
	}
	
	//其他国家移民项目
	private function getQita(){
		$qita = M("Qita_cate")->select();	
		$this->right['qita'] = $qita;		
	}
	
	//右侧
	public function right($cid="",$isshow=''){
		$this->getSubsidiaries();//全球旭飞
		$this->getRightNews();
		$this->getRightProject();
		$this->getQita();
		return $this->right;	
	}
		
}